<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <title>releaseNoteEditor</title>
    <script type="text/ecmascript" src="assets/vendor/jquery/jquery.3.2.1.min.js"></script>

    <link rel="stylesheet" href="assets/vendor/bootstrap-4.3.1-dist/css/bootstrap.min.css" />
</head>

<body>
    <div class='container-fluid' style="height:100vh">
        <div class="row">
            <div class="col-8">
                <h3>Flaw Scan Analysis Release Note</h3>
            </div>
            <div class="col-4 input-group" id="version" style="display:none">
                <div class="input-group-prepend">
                    <span class="input-group-text">version:</span>
                </div>
                <input class="form-control" id="versionInput" type="text" placeholder="version"></input>
            </div>
        </div>
        <div class="row" style="height:100%">
            <input id="releasenoteObjid" type="hidden" value="${releasenote_id}" />
            <%@ include file="../common/mdEditor.jsp"%>
        </div>
    </div>
    <script>
        $(function () {
            content = '';
            releasenote = '';
            isNew = false;
            objId = $("#releasenoteObjid").val();
            if (Number(objId) === -1) {
                isNew = true;
                $('#version').show();
                fonsave = function () {
                    val = $('.editormd-markdown-textarea').val();
                    version = $('#versionInput').val();
                    if (!version) {
                        alert("please input version");
                        return;
                    }
                    dt = new Date().toLocaleString().replace(',', ''); // "4/24/2021 2:38:10 PM"
                    $.ajax({
                        url: 'releaeNote/newReleaseNote.do',
                        method: 'POST',
                        data: {
                            version: version,
                            content: val,
                            publishDate: dt,
                        },
                        success: function (rst) {
                            if (rst.success) {
                                alert('release note has been created.');
                            } else {
                                alert('Error,look server output.');
                            }
                            window.location.href = "ReleaseNoteAdmin.do";
                        }
                    })
                };
                mySetting = {
                    imageUpload: true, //allow to upload images
                    imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"], //image extantino
                    // imageUploadURL: "releaseNote/image/upload.do", //upload url
                    imageUploadURL: "image/upload.do",
                    onsave: fonsave,
                    tocStartLevel: 2 // default is 1
                };
                createEditor("", mySetting);
            } else {
                fonsave = function () {
                    val = $('.editormd-markdown-textarea').val();
                    $.ajax({
                        url: 'releaeNote/updateReleaseNote.do',
                        method: 'POST',
                        data: {
                            objId: objId,
                            content: val
                        },
                        success: function (rst) {
                            if (rst.success) {
                                alert('release note has been updated.');
                            } else {
                                alert('Error,look server output.');
                            }
                            window.location.href = "ReleaseNoteAdmin.do";
                        }
                    })
                };
                $.ajax({
                    method: 'POST',
                    url: 'getReleaseNote.do',
                    data: {
                        objId: objId
                    },
                    dataType: 'json',
                    success: function (data) {
                        releasenote = data.rtnVal
                        content = releasenote.content;

                        mySetting = {
                            imageUpload: true, //allow to upload images
                            imageFormats: ["jpg", "jpeg", "gif", "png", "bmp",
                                "webp"
                            ], //image extantino
                            // imageUploadURL: "releaseNote/image/upload.do", //upload url
                            imageUploadURL: "image/upload.do",
                            onsave: fonsave,
                            tocStartLevel: 2 // default is 1
                        };
                        createEditor(content, mySetting);
                    }
                });
            }
        });
    </script>

</body>

</html>